#include <iostream>
#include <filesystem>
#include <fmt/core.h>
#include <string>

std::string getFileMd5Sum(const std::string& filepath) 
{
    if (!std::filesystem::exists(filepath)) {
        return std::string();
    }
    
    std::string command = fmt::format(R"(md5sum "{}")", filepath);
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe) return std::string();
    
    char buffer[128];
    std::string result;
    while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
        result += buffer;
    }
    pclose(pipe);
    
    // 提取纯MD5值
    size_t idx = result.rfind(' ');
    if (idx != std::string::npos) {
        return result.substr(0, idx);
    }
    return result;
}

int main() 
{
    std::string ret = getFileMd5Sum("test.txt");
    std::cout << ret << '\n';
    return 0;
}